Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RateLimitChecker #5521

Merged
merged 8 commits into from
Jan 20, 2020
Merged

Conversation

citizen428
Copy link
Contributor

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

The current rate limit checker uses hard-coded limits for everything, and the intention of this PR is to make it externally configurable. Additionally the code will be cleaned up a bit.

Related Tickets & Documents

#5520

Added to documentation?

  • docs.dev.to
  • readme
  • no documentation needed

@citizen428
Copy link
Contributor Author

This is a WIP/discussion PR for now, I'll post comments inline.

app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
app/labor/rate_limit_checker.rb Show resolved Hide resolved
app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
@rhymes rhymes removed their request for review January 15, 2020 10:47
Copy link
Contributor

@rhymes rhymes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments

app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
app/labor/rate_limit_checker.rb Show resolved Hide resolved
app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
app/labor/rate_limit_checker.rb Outdated Show resolved Hide resolved
false
end
check_method = "check_#{action}_limit"
result = respond_to?(check_method, true) ? send(check_method) : false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the true as second argument to respond_to? so it also considers private methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL that respond_to? takes a second parameter!

@citizen428 citizen428 changed the title [WIP] Refactor RateLimitChecker Refactor RateLimitChecker Jan 16, 2020
@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Jan 16, 2020
Comment on lines +23 to +25
field :rate_limit_comment_creation, type: :integer, default: 9
field :rate_limit_published_article_creation, type: :integer, default: 9
field :rate_limit_image_upload, type: :integer, default: 9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to submit another PR after this to expose these rate limits to /internal/config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was torn on whether or not to do it in the same PR. In the end my reasoning was that I can finish the refactoring first, since the current version is also non-configurable. Then add the configuration options as separate PR, which has the advantage that the PRs are more limited in scope and therefore easier to review. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that works for me!

create_list(:comment, 10, user_id: user.id, commentable_id: article.id)
expect(described_class.new(user).limit_by_action("comment_creation")).to eq(true)
count = SiteConfig.rate_limit_comment_creation + 1
create_list(:comment, count, user_id: user.id, commentable_id: article.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested but would it be possible to mock or lower the rate limit as a precondition? Creating 10 comments in the DB when we can create just 2 or 3 it's a waste :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, reduced the number of objects created.

@citizen428
Copy link
Contributor Author

@rhymes Thanks for the review. I also noted that we're still missing one SiteConfig for number of emails to the same recipient, I added that now.

Copy link
Contributor

@rhymes rhymes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@pr-triage pr-triage bot added PR: reviewed-approved bot applied label for PR's where reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Jan 20, 2020
Copy link
Contributor

@mstruve mstruve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

false
end
check_method = "check_#{action}_limit"
result = respond_to?(check_method, true) ? send(check_method) : false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL that respond_to? takes a second parameter!

@mstruve mstruve merged commit 511f741 into master Jan 20, 2020
@pr-triage pr-triage bot removed the PR: reviewed-approved bot applied label for PR's where reviewer approves changes label Jan 20, 2020
@mstruve mstruve deleted the citizen428/refactor-rate-limit-check-5520 branch January 20, 2020 16:27
@pr-triage pr-triage bot added the PR: merged bot applied label for PR's that are merged label Jan 20, 2020
citizen428 added a commit that referenced this pull request Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants